<template>
    <div class='approval'>
      <el-card class="box-card">
        <div class="title">{{$t('customerService.jqsj')}}</div>
        <div class="recent-data">
          <!-- 今日已分配订单 -->
          <div class="recent-item" @click="handleRecentData(1)">
            <div>
              <b>{{recentData[1]}}</b>
            </div>
            <div>{{$t('customerService.jryfpdd')}}</div>
          </div>
          <!-- 今日已回访订单 -->
          <div class="recent-item" @click="handleRecentData(2)">
            <div>
              <b>{{recentData[2]}}</b>
            </div>
            <div>{{$t('customerService.jryhfdd')}}</div>
          </div>
          <!-- 今日未回访订单 -->
          <div class="recent-item" @click="handleRecentData(3)">
            <div>
              <b>{{recentData[3]}}</b>
            </div>
            <div>{{$t('customerService.jrwhfdd')}}</div>
          </div>
          <!-- 今日已退案订单 -->
          <div class="recent-item" @click="handleRecentData(4)">
            <div>
              <b>{{recentData[4]}}</b>
            </div>
            <div>{{$t('customerService.jrytadd')}}</div>
          </div>
        </div>
        <div class="recent-data">
          <!-- 历史已分配订单 -->
          <div class="recent-item" @click="handleRecentData(5)">
            <div>
              <b>{{recentData[5]}}</b>
            </div>
            <div>{{$t('customerService.lsyfpdd')}}</div>
          </div>
          <!-- 历史已回访订单 -->
          <div class="recent-item" @click="handleRecentData(6)">
            <div>
              <b>{{recentData[6]}}</b>
            </div>
            <div>{{$t('customerService.lsyhfdd')}}</div>
          </div>
          <!-- 历史未回访订单 -->
          <div class="recent-item" @click="handleRecentData(7)">
            <div>
              <b>{{recentData[7]}}</b>
            </div>
            <div>{{$t('customerService.lswhfdd')}}</div>
          </div>
          <!-- 历史已退案订单 -->
          <div class="recent-item" @click="handleRecentData(8)">
            <div>
              <b>{{recentData[8]}}</b>
            </div>
            <div>{{$t('customerService.lsytadd')}}</div>
          </div>
        </div>
      </el-card>
        <el-card class="box-card">
            <div class='title'>
                {{$t('approvalDispatch.title1')}}
            </div>
            <div class='menu-search'> 
                <div class='menu-title'>
                  <!-- 时间 -->
                  <div class='searchItem searchItem-date'>
                    <span>{{$t('approvalDispatch.crtTime')}}</span>
                    <el-date-picker
                    size='small'
                    v-model="listQuery.startDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    :placeholder="$t('rule.beginDate')">
                    </el-date-picker>
                    <b>{{$t('approval.go')}}</b>
                    <el-date-picker
                    size='small'
                      v-model="listQuery.endDate"
                      type="date"
                      value-format="yyyy-MM-dd"
                      :placeholder="$t('rule.endDate')">
                    </el-date-picker>
                  </div>
                   <!-- 业务ID -->
                  <div class='searchItem'>
                      <span>{{$t('customerService.orderId')}}</span>
                      <el-input v-model='listQuery.orderId' :placeholder="$t('rule.orderId')" size='small'></el-input>
                  </div>
                  <!-- 处理状态 -->
                  <div class='searchItem'>
                    <span>{{$t('customerService.clzt')}}</span>
                    <el-select v-model='listQuery.status' size='small'>
                      <el-option
                      v-for='item in statusList'
                      :key='item.value'
                      :label='item.label'
                      :value='item.value'>
                      </el-option>
                    </el-select>
                  </div>
                  <!-- 业务渠道 -->
                  <div class='searchItem'>
                    <span>{{$t('customerService.channel')}}</span>
                    <el-select v-model='listQuery.channel' size='small' @change="handleChannelChange">
                      <el-option
                      v-for='item in channelList'
                      :key='item.channel'
                      :label='item.org_name'
                      :value='item.channel'>
                      </el-option>
                    </el-select>
                  </div>
                  <!-- 客服组 -->
                  <div class='searchItem'>
                    <span>{{$t('customerService.kfz')}}</span>
                    <el-select 
                    v-model='listQuery.customGroupId' 
                    size='small'
                    @change="handleServiceGroupChange">
                      <el-option
                      v-for='item in serviceGroup'
                      :key='item.channel'
                      :label='item.value'
                      :value='item.label'>
                      </el-option>
                    </el-select>
                  </div>
                  <!-- 客服员 -->
                  <div class='searchItem'>
                    <span>{{$t('customerService.kfy')}}</span>
                    <el-select v-model='listQuery.customId' size='small'>
                      <el-option
                      v-for='item in serviceIds'
                      :key='item.channel'
                      :label='item.value'
                      :value='item.label'>
                      </el-option>
                    </el-select>
                  </div>
                  <!-- 客户姓名 -->
                  <div class='searchItem'>
                    <span>{{$t('customerService.customerName')}}</span>
                    <el-input v-model='listQuery.userName' :placeholder="$t('rule.realName')" size='small'></el-input>
                  </div>
                   <!-- 手机号 -->
                  <div class='searchItem'>
                    <span>{{$t('customerService.phoneNo')}}</span>
                    <el-input v-model='listQuery.userPhone' :placeholder="$t('rule.phoneNo')" size='small'></el-input>
                  </div>
                </div>
                <div class='menu-title-btn'>
                  <button type='button' @click='search'>
                      {{$t('btn.query')}}
                  </button>
                  <button type='button' @click='reset'>
                      {{$t('btn.reset')}}
                  </button>
              </div>
            </div>
        </el-card>
        <el-card class='box-card'>
            <div class='title'>
                {{$t('approvalDispatch.title2')}}
            </div>
            <el-table
                v-loading="listLoading"
                ref='multipleTable'
                :data='tableData'
                tooltip-effect='dark'
                style='width: 100%'
                @selection-change='handleSelectionChange'
                border
                size='mini'>
                <!-- 业务id -->
                <el-table-column
                prop='orderId' align="center" header-align="center" 
                :label="$t('customerService.orderId')"
                width='140'>
                </el-table-column>
                <!-- 审批额度 -->
                <el-table-column
                prop='loanAmt' align="center" header-align="center" 
                :label="$t('customerService.apprAmt')"
                :formatter="cashFormatter"
                width='180'>
                </el-table-column>
                <!-- 放款金额 -->
                <el-table-column
                prop='lentAmt' align="center" header-align="center" 
                :label="$t('customerService.loanAmt')"
                :formatter="cashFormatter"
                width='180'>
                </el-table-column>
                <!-- 放款成功时间 -->
                <el-table-column
                prop='cashoutDate' align="center" header-align="center" 
                :label="$t('customerService.successTime')"
                width='140'>
                </el-table-column>
                <!-- 借款期限 -->
                <el-table-column
                prop='pTerm' align="center" header-align="center" 
                :label="$t('customerService.loanTerm')"
                width='100'>
                </el-table-column>
                <!-- 客户姓名 -->
                <el-table-column
                prop="userRealName" align="center" header-align="center" 
                :label="$t('customerService.customerName')"
                width='200'>
                </el-table-column>
                <!-- 手机号 -->
                <el-table-column
                prop='userPhone' align="center" header-align="center" 
                :label="$t('customerService.phoneNo')"
                width='200'>
                </el-table-column>
                <!-- 借款App -->
                <el-table-column
                prop='appPackageHeader' align="center" header-align="center" 
                :label="$t('approvalDetail.loanApp')"
                width='140'>
                </el-table-column>
                <!-- 业务渠道 -->
                <el-table-column
                prop='channelDesc' align="center" header-align="center" 
                :label="$t('customerService.channel')"
                width='200'>
                </el-table-column>
                <!-- 处理状态 -->
                <el-table-column
                prop='status' align="center" header-align="center" 
                :label="$t('approval.status')"
                :formatter="statusFormat"
                width='200'>
                </el-table-column>
                <!-- 回访客服 -->
                <el-table-column
                prop='serviceName' align="center" header-align="center" 
                :label="$t('customerService.hfkf')"
                width='200'>
                </el-table-column>
                <!-- 客服组 -->
                <el-table-column
                prop='serviceGroupName' align="center" header-align="center" 
                :label="$t('customerService.kfz')"
                width='200'>
                </el-table-column>
                <!-- 分配时间 -->
                <el-table-column
                prop='distributionTime' align="center" header-align="center" 
                :label="$t('customerService.fpsj')"
                width='200'>
                </el-table-column>
                <!-- 回访时间 -->
                <el-table-column
                prop='dealTime' align="center" header-align="center" 
                :label="$t('customerService.hfsj')"
                width='200'>
                </el-table-column>
                <!-- 备注 -->
                <el-table-column
                prop='checkRemark' align="center" header-align="center" 
                :label="$t('user.remarks')"
                width='200'>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
              <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="listQuery.page"
              :page-sizes="[5, 10, 20, 30]"
              :page-size="listQuery.limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
              </el-pagination>
            </div>
        </el-card>
    </div>
</template>

<script>
import { mapGetters } from 'vuex';
import { formatCash } from 'utils/index'
import { getChannels, getCustomerGroup, getCustomer, rencentAmount, page } from '@/api/customerService/index'
export default {
  name: 'DistributedOrder',
  data() {
    return {
      listLoading: false,
      total: null,
      listQuery: {
        limit: 5,
        page: 1,
        // 分配状态 1:未分配2：已分配
        distributionStatus: 2,
        // 放款开始时间
        startDate: '',
        // 放款结束时间
        endDate: '',
        // 渠道
        channel: '',
        // 订单id
        orderId: '',
        // ⽤用户⼿手机号
        userPhone: '',
        // 客户姓名
        userName: '',
        // 客服组id
        customGroupId: '',
        // serviceGroupId: '',
        // 客服员id
        customId: '',
        // 处理理状态
        status: '',
        // 近期数据类型
        type: ''
      },
      recentData: {
        // 今⽇日已分配订单
        1: 0,
        // 今⽇日已回访订单
        2: 0,
        // 今⽇日未回访订单
        3: 0,
        // 今⽇日 已退案订单
        4: 0,
        // 历史已分配订单
        5: 0,
        // 历史已回访订单
        6: 0,
        // 历史未回访 订单
        7: 0,
        // 历史已退案订单
        8: 0
      },
      tableData: [],
      multipleSelection: [],
      dialogFormVisible: false,
      statusList: [{
        value: -1,
        label: this.$t('customerService.all')
      }, {
        value: 1,
        label: this.$t('customerService.wcl')
      }, {
        value: 2,
        label: this.$t('customerService.yjt')
      }, {
        value: 3,
        label: this.$t('customerService.wjt')
      }, {
        value: 4,
        label: this.$t('customerService.ta')
      }],
      // 渠道列表
      channelList: [],
      // 客服组列表
      serviceGroup: [],
      // 客服⼈列表
      serviceIds: []
    };
  },
  computed: {
    ...mapGetters(['elements'])
  },
  created() {
    this.getRecentData()
    this.initDate()
    // 获取渠道列表
    getChannels().then(res => {
      if (res.status === 200 && res.data && res.data.length > 0) {
        this.channelList = this.channelList.concat(res.data)
      }
    })
    this.getList()
  },
  methods: {
    statusFormat(row, col, cellValue) {
      let statusName = ''
      this.statusList.forEach(item => {
        if (item.value === parseInt(cellValue)) {
          statusName = item.label
        }
      })

      return statusName
    },
    cashFormatter(row, col, cellValue) {
      return formatCash(cellValue)
    },
    handleRecentData(type) {
      this.listQuery.page = 1;
      this.listQuery.type = type;
      this.getList()
    },
    // 近期数据
    getRecentData() {
      rencentAmount().then(res => {
        if (res.status === 200) {
          const data = res.data
          data.forEach(item => {
            this.recentData[item.type] = item.value
          })
        }
      })
    },
    // 渠道联动
    handleChannelChange(val) {
      getCustomerGroup(val).then(res => {
        this.listQuery.serviceGroupId = ''
        this.listQuery.serviceId = ''
        this.serviceGroup = []
        this.serviceIds = []
        if (res.status === 200 && res.data && res.data.length > 0) {
          this.serviceGroup = this.serviceGroup.concat(res.data)
        }
      })
    },
    // 客服组联动
    handleServiceGroupChange(val) {
      const data = {
        channel: this.listQuery.channel,
        groupId: val
      }
      getCustomer(data).then(res => {
        this.serviceId = ''
        this.serviceIds = []
        if (res.status === 200 && res.data && res.data.length > 0) {
          this.serviceIds = this.serviceIds.concat(res.data)
        }
      })
    },
    // 分页数组查询
    getList() {
      if (this.listQuery.startDate === '' || this.listQuery.endDate === '') {
        this.$message({
          showClose: true,
          message: this.$t('customerService.dateRangeError'),
          type: 'warning'
        });
        return false
      }

      this.listLoading = true;
      page(this.listQuery).then(res => {
        this.listLoading = false
        if (res.status === 200) {
          this.tableData = res.data.rows
          this.total = parseInt(res.data.total)
        }
      })
    },
    // 查询
    search() {
      this.listQuery.page = 1;
      this.listQuery.type = ''
      this.getList();
    },
    // 重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.listQuery.channel = ''
      this.listQuery.orderId = ''
      this.listQuery.userPhone = ''
      this.listQuery.userName = ''
      this.initDate()
    },
    // 日期初始化
    initDate() {
      this.listQuery.startDate = this.GetDateStr(-7);
      this.listQuery.endDate = this.GetDateStr(-1);
    },
    GetDateStr(AddDayCount) {
      const dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);// 获取AddDayCount天后的日期
      const y = dd.getFullYear();
      const m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1); // 获取当前月份的日期，不足10补0
      const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
      return y + '-' + m + '-' + d;
    },
    // 选中的数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before{
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px  0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px  0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
       width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.handler {
  width: 90%;
  display: flex;
  justify-content: space-between;
  align-items: center;
    span {
      display: inline-block;
      width: 80px;
      text-align: center;
      font-size: 14px;
      font-weight: bold;
    }
    div:nth-child(2) {
      flex: 1;
    }
}
.menu-list {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.bg {
  display: inline-block;
  border-radius: 5px;
  border: 2px solid #fff;
  width: 25px;
  height: 25px;
  cursor: pointer;
}
.color1 {
  background: #ec7070;
}
.color2 {
  background: #73d466;
}
.type-marker {
  display: flex;
  align-items: center;
  margin-right: 20px;
}
.recent-data {
  display: flex;
  flex-wrap: wrap;
  font-size: 14px;
  margin-bottom: 10px;
  .recent-item {
    height: 70px;
    display: flex;
    flex: 1;
    flex-direction: column;
    justify-content: center;
    border: 1px solid #aaa;
    text-align: center;
    border-radius: 10px;
    margin-top: 10px;
    margin-right: 10px;
    margin-left: 10px;
    // margin-right: 40px;
    padding: 10px 20px;
    box-sizing: border-box;
    cursor: pointer;
    & > div:nth-child(1) {
      line-height: 24px;
      b {
        font-size: 18px;
      }
    }
  }
}

</style>